package com.lhh.userservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.History;
import com.lhh.springcommon.entity.vo.VideoVO;
import com.lhh.springcommon.mapper.HistoryMapper;
import com.lhh.springcommon.service.IHistoryService;
import com.lhh.springcommon.utils.PageQuery;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.userservice.service.UserHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 李厚航
 * @date 2025-03-01 22:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserHistoryServiceImpl implements UserHistoryService {

    private final IHistoryService historyService;

    private final HistoryMapper historyMapper;

    @Autowired
    public UserHistoryServiceImpl(IHistoryService historyService, HistoryMapper historyMapper) {
        this.historyService = historyService;
        this.historyMapper = historyMapper;
    }

    @Override
    @WebLog
    @Cacheable(value = "queryPageHistory", key = "#page + ':' + #pageSize + ':' + #uid")
    public Page<VideoVO> queryPageHistory(Integer page,Integer pageSize,Integer uid) {
        History history = new History();
        history.setUid(uid);
        Page<History> historyPage = new Page<>(page, pageSize);
        return historyMapper.queryPageHistoryVideo(history, historyPage);
    }

    @Override
    @WebLog
    @CacheEvict(value = "queryPageHistory",allEntries = true)
    public Boolean addHistory(History history) {
        History existingHistory = historyService.getOne(new LambdaQueryWrapper<History>()
                .eq(history.getUid() != null, History::getUid, history.getUid())
                .eq(history.getVid() != null, History::getVid, history.getVid()));
        if(existingHistory!=null){
            existingHistory.setCreateTime(LocalDateTime.now());
            return historyService.updateById(existingHistory);
        }
        return historyService.save(history);
    }

    @Override
    @WebLog
    @CacheEvict(value = "queryPageHistory",allEntries = true)
    public Boolean deleteHistory(History history) {
        return historyService.remove(new LambdaQueryWrapper<History>()
                .eq(history.getId() != null,History::getId,history.getId())
                .eq(history.getUid() != null,History::getUid,history.getUid())
                .eq(history.getVid() != null,History::getVid,history.getVid()));
    }

    @Override
    @WebLog
    public Boolean isHistory(Integer uid,Integer vid) {
        return historyService.getOne(new LambdaQueryWrapper<History>()
                .eq(History::getUid, uid)
                .eq(History::getVid, vid)) != null;
    }
}
